package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.Classes;
import com.thermal.thermal_inspection.entity.RealName;
import com.thermal.thermal_inspection.entity.Unit;
import com.thermal.thermal_inspection.service.AccountService;
import com.thermal.thermal_inspection.service.ClassService;
import com.thermal.thermal_inspection.service.UnitService;
import com.thermal.thermal_inspection.vo.RealNameVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/users")
public class SearchController {
    private static final Logger logger = Logger.getLogger(SearchController.class);

    @Autowired
    AccountService accountService;
    @Autowired
    UnitService unitService;
    @Autowired
    ClassService classService;

    /**
     * 搜索指定用户
     */
    @GetMapping("/account/search")
    public CommonResponse<List<RealNameVO>> getSpecifiedUser(@RequestParam(value = "userId", required = false) Integer userId,
                                                             @RequestParam(value = "realName", required = false) String realName){
        List<RealNameVO> realNameVOList = accountService.getSpecifiedUser(userId, realName);
        if(realNameVOList.isEmpty()){
            logger.error("搜索指定用户失败");
            return CommonResponse.createForError("搜索指定用户失败");
        }
        logger.info("搜索指定用户成功");
        return CommonResponse.createForSuccess("SUCCESS", realNameVOList);
    }

    /**
     * 搜索指定班组
     */
    @GetMapping("/class/search")
    public CommonResponse<List<Classes>> getSpecifiedClass(@RequestParam(value = "className", required = false) String className,
                                                           @RequestParam(value = "classId", required = false) Integer classId){
        List<Classes> classList = classService.getSpecifiedClass(classId, className);
        if(classList.isEmpty()){
            logger.error("搜索指定班级失败");
            return CommonResponse.createForError("搜索指定班级失败");
        }
        logger.info("搜索指定班级成功");
        return CommonResponse.createForSuccess("SUCCESS", classList);
    }

    /**
     * 搜索指定机组
     */
    @GetMapping("/unit/search")
    public CommonResponse<List<Unit>> getSpecifiedUnit(@RequestParam(value = "unitName", required = false) String unitName,
                                                        @RequestParam(value = "unitId", required = false) Integer unitId){
        List<Unit> unitList = unitService.getSpecifiedUnit(unitId, unitName);
        if(unitList.isEmpty()){
            logger.error("搜索指定机组失败");
            return CommonResponse.createForError("搜索指定机组失败");
        }
        logger.info("搜索指定机组成功");
        return CommonResponse.createForSuccess("SUCCESS", unitList);
    }
}
